﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAO;

namespace TAM_SPORT
{
    public partial class ChangePassForUser : Form
    {
        private int id;
        private UserDao user;
        public ChangePassForUser()
        {
            InitializeComponent();
        }

        public ChangePassForUser(int id)
        {
            this.id = id;
            InitializeComponent();
            user = new UserDao();
            onload();
        }

        private void onload()
        {
            DataRow row = user.getUserById(id);
            if (row != null)
            {
                txtFullName.Text = row["FULL_NAME"].ToString();
                txtUserName.Text = row["USR_NAME"].ToString();
                if (row["AD_FLG"].ToString().Equals("T"))
                {
                    cbxAdmin.Checked = true;
                }
                else
                {
                    cbxAdmin.Checked = false;
                }

            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string admin = "";
            if (cbxAdmin.Checked)
            {
                admin = "T";
            }
            else
            {
                admin = "F";
            }
            if (txtNewPass.Text.Trim() == ""
                && txtCornfirmPass.Text.Trim() == "" && !string.IsNullOrEmpty(txtFullName.Text))
            {
                if (user.changeAdminStatus(id, txtFullName.Text.Trim() ,admin))
                {
                    Result r = new Result("Thay đổi thông tin thành công.");
                    r.ShowDialog();
                    r.Dispose();
                    this.Dispose();
                }
                else
                {
                    Error er = new Error("Thay đổi thông tin thất bại.");
                    er.ShowDialog();
                    er.Dispose();
                }
            }
            else
            {
                if (txtCornfirmPass.Text.Trim().Equals(txtNewPass.Text.Trim()))
                {
                    if (user.changPassForUser(id, txtNewPass.Text.Trim(), txtFullName.Text.Trim(), admin))
                    {
                        Result r = new Result(UserConstant.CHANGE_PASS_SUCCESS);
                        r.ShowDialog();
                        r.Dispose();
                        this.Dispose();
                    }
                    else
                    {
                        Error er = new Error(UserConstant.CHANGE_PASS_FAIL);
                        er.ShowDialog();
                        er.Dispose();
                    }
                }
                else
                {
                    Error er = new Error(UserConstant.USER_CONFIRM_PASS);
                    er.ShowDialog();
                    er.Dispose();
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
